package Contorller;
import java.io.IOException;
import java.io.OutputStream;

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.SerialPort;



public class ControlePorta {
	
	private OutputStream serialOut;
	
	 public void initSerial() {
	    	
	    	try {
	    		CommPortIdentifier portId = null;
	    		try {
			        portId = CommPortIdentifier.getPortIdentifier("COM4");
	    		} catch (NoSuchPortException npe) {
	    			portId = CommPortIdentifier.getPortIdentifier("COM3");
	    		}

		        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
		        serialOut = port.getOutputStream();
		        port.setSerialPortParams(9600,
		                SerialPort.DATABITS_8,
		                SerialPort.STOPBITS_1,
		                SerialPort.PARITY_NONE);
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}
	    }
	 
	 public void close(){
		 try {
			serialOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 
	 public void enviaDados(int opcao){
		 byte[] data = new byte[2];
		if(opcao == 0){
			data[0] = (byte)1;
	 		data[1] = (byte)1;
		}else{
			data[0] = (byte)2;
	 		data[1] = (byte)2;
		} 
 		
		 try {
			serialOut.write(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}